let App = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile: "",
    show: false,
    getdata: {},
    sercherStorage: [],
    StorageFlag: false, //显示搜索记录标志位
    is_display: 'none'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 删除用户输入
   */
  emptyInput: function () {
    var that = this;
    that.setData({
      mobile: ""
    })
  },
  tapSercherStorage: function (e) {
    var that = this;
    var val = e.currentTarget.dataset.name;
    that.setData({
      mobile: val,
      is_display: "block"
    })
    that.getdata();
  },
  /**
   * 获取用户输入 
   */
  mobile: function (e) {
    this.setData({
      mobile: e.detail.value
    })
    // console.log(this.data.mobile)
    if (this.data.mobile != '') {
      this.setData({
        is_display: ''
      })
    }
  },
  //清除缓存历史
  clearSearchStorage: function () {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确定清除搜索历史吗',
      success: function (res) {
        if (res.confirm) {
          wx.removeStorageSync('searchData')
          that.setData({
            sercherStorage: [],
            StorageFlag: false,
          })
        }

      }
    });


  },
  //打开历史记录列表
  openLocationsercher: function () {
    this.setData({
      sercherStorage: wx.getStorageSync('searchData') || [],
      StorageFlag: true,
    })
  },
  /**
   *用户点击获取数据事件 
   */
  getdata: function (e) {
    var that = this;
    var mobile = this.data.mobile;



    //控制搜索历史
    if (that.data.mobile != '' && that.data.sercherStorage.indexOf(mobile) < 0) {
      //将搜索记录更新到缓存
      var searchData = that.data.sercherStorage;
      searchData.push(
        that.data.mobile
      )
      wx.setStorageSync('searchData', searchData);
      that.setData({
        StorageFlag: false,
      })
    }
    //请求查询
    wx.showLoading({
      title: '查询中',
    });
    App._get('search_mobile', {
      mobile: that.data.mobile
    }, result => {

      if (result.data.code == "0") {
        that.setData({
          show: true,
          StorageFlag: false,
          getdata: result.data.data
        })
      } else {
        that.setData({
          show: false
        })
        App.showSuccess("暂无信息!")
      }
    },function(){},function(){
      
      wx.hideLoading();
    });
    
  }
})